package br.com.felinelayer.server.application;

import br.com.felinelayer.common.Message;
import br.com.felinelayer.common.Protocol;
import java.util.ArrayList;

/**
 *
 * @author Vanius Roberto Bittencourt
 */
public class Frame extends Component<Frame> {

    public Frame setCurrentRow(int currentRow) {
        this.currentRow = currentRow;
        return this;
    }
    private int currentRow = 0;
    private int currentCol = 0;

    public int getCurrentRow() {
        return currentRow;
    }

    public Frame setCurrentCol(int currentCol) {
        this.currentCol = currentCol;
        return this;
    }

    public int getCurrentCol() {
        return currentCol;
    }

    public Frame addRow() {
        ++currentRow;
        currentCol = 0;
        return this;
    }
    private ArrayList<Component> components = new ArrayList<>();

    public <T extends Component> T addComponent(T component) {
        components.add(component);
        component.setFrame(this);
        component.setRow(currentRow);
        component.setCol(currentCol);
        return component;
    }

    public Button addButton() {
        return addComponent(application.addButton());
    }

    public ComboBox addComboBox() {
        return addComponent(application.addComboBox());
    }

    public Edit addEdit() {
        return addComponent(application.addEdit());
    }

    public Grid addGrid() {
        return addComponent(application.addGrid());
    }
    public Label addLabel() {
        return addComponent(application.addLabel());
    }

    public void show() {
        Message m = new Message(Protocol.MSG_NAME_METHOD);
        m.setValueByName(Protocol.METHOD_NAME, Protocol.METHOD_FRAME_SHOW);
        inquireMessage(m);
    }

    public void close() {
        for (Component c : components) {
            c.clearListeners();
        }
        Message m = new Message(Protocol.MSG_NAME_METHOD);
        m.setValueByName(Protocol.METHOD_NAME, Protocol.METHOD_FRAME_CLOSE);
        inquireMessage(m);
    }

    @Override
    public String getComponentClassName() {
        return Protocol.COMPONENT_FRAME;
    }
}
